String.prototype.replaceall = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
    }

function lang(id) { 
   switch(id){       
   case "vi-en":
       return "Vietnamese-English";
   case "vi-vi":
       return "Vietnamese-Vietnamese";
   default:
       return "English-Vietnamese";           
   }
}

function hideBox(id){
	mybox = "#box" + id;
    jQuery(mybox).slideToggle("slow");
}


function getsupportedprop(proparray) {
    var root = document.documentElement //reference root element of document
    for (var i = 0; i < proparray.length; i++) { //loop through possible properties
        if (typeof root.style[proparray[i]] == "string") { //if the property value is a string (versus undefined)
            return proparray[i] //return that string
        }
    }
}

function setFocus(id) {
    document.getElementById(id).focus();
}

function clearOnFocus(id) {
    var t = document.getElementById(id);
    //set box-shadow property css3
    var boxshadowprop = getsupportedprop(['boxShadow', 'MozBoxShadow', 'WebkitBoxShadow']); //get appropriate CSS3 box-shadow property
    t.style[boxshadowprop] = "2px 2px 2px #FFFFFF"; //set CSS shadow for t
    switch (id) {
        case "acc":
            {
                if (t.value == "Username ...") {
                    t.value = "";
                }
                break;
            }

        case "psw":
            {
                if (t.value == "Password ...") {
                    t.value = "";
                }
                break;
            }
        default:
            {
                if (t.value == "Enter your word ...") {
                    t.value = "";
                }
            }
    }
}
function showOnBlur(id) {
    var t = document.getElementById(id);
    //set box-shadow property css3
    var boxshadowprop = getsupportedprop(['boxShadow', 'MozBoxShadow', 'WebkitBoxShadow']) ;//get appropriate CSS3 box-shadow property
    t.style[boxshadowprop] = "0px 0px 0px #0171A3" ;//set CSS shadow for t
    switch (id) {
        case "acc":
            {
                if (t.value == "") {
                    t.value = "Username ...";
                }
                break;
            }

        case "psw":
            {
                if (t.value == "") {
                    t.value = "Password ...";
                }
                break;
            }
        default:
            {
                if (t.value == "") {
                    t.value = "Enter your word ...";
                }
            }
    }
}



function validatesearchfrm() {
    var w = document.getElementById("w");
    if (w.value != '' && isNaN(w.value))
        return true;
    return false;
}

function validateregfrm() {
    var user = document.getElementById("acc");
    var psw = document.getElementById("psw");
    if (user != "" && isNaN(user) && psw != "" && isNaN(psw)) {
        return true;
    }

    return false;
}

function Lookup() { 
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            try {               
                var words = eval("(" + xmlhttp.responseText + ")");
                var l = words.length;
                var html = "";
                for (i = 0; i < l; i++) {					
					var x = words[i].dinhnghia;				
					x=x.replaceall("+", ":");		
					x=x.replaceall("=", "#");
					x=x.replaceall("@", "</li><li class='ia'>");
					x=x.replaceall("*", "</li><li class='ib'>");
					x=x.replaceall("- ", "</li><li class='ic'>");
					x=x.replaceall("!", "</li><li class='id'>");
					x=x.replaceall("#", "</li><li class='ie'>");					
                    html += "<div onmouseout='onOut("+i+")' onmouseover='onOver("+i+")'  class='box' id='box" + i + "'>";
                    html += "<div class='hd' id='hd" + i + "'>";
                    html += "<span class='tudien' id='title" + i + "'>" + lang(words[i].tudien);
                    html += "</span>";
                    html += "<span onclick='hideBox("+i+")' class='cmd' id='cmd" + i + "'>";
					html += "<a href='#' class='img' id='img"+i+"'>X</a>";
                    html += "</span>";
                    html += "</div>";
                    html += "<div class='bd' id='bd" + i + "'>";                 
                    html += "<div class='dinhnghia' id='dinhnghia" + i + "'>"  + x;
                    html += "</div>";
                    html += "</div>";
                    html += "</div>";
                }                
                document.getElementById("resultbox").innerHTML = html;				
                document.getElementById("waiting").innerHTML = "";				
            }
            catch (err) {
                document.getElementById("resultbox").innerHTML = "";
                document.getElementById("waiting").innerHTML = "";
            }
        }
        else {
            document.getElementById("waiting").innerHTML = "please waiting...";
        }
    }
    var w = document.getElementById("w");
    xmlhttp.open("GET", "/c/lookup.php?w=" + w.value, true);
    xmlhttp.send(null);
}


function Lookup2(value)
{
    var w = document.getElementById("w");
    w.value=value;
    Lookup();
}
function onOver(id) {
    document.getElementById("cmd" + id).style.visibility = 'visible';
}

function onOut(id) {
    document.getElementById("cmd" + id).style.visibility = 'hidden';   
}
function getSelText()
{
    var txt = '';
   if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
    return txt;
}
  function getCookie(c_name){
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) 
                c_end = document.cookie.length
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
function setCookie(c_name, value, expiredays){
    var exdate = new Date(2011, 11, 25);
    exdate.setDate(exdate + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString());
}

function delCookie(name, path, domain){
    if (getCookie(name)) 
        document.cookie = name + "=" +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

 
function mouseUP(e) {
 var ctrlPressed=0;
 var altPressed=0;
 var shiftPressed=0;
 if (parseInt(navigator.appVersion)>3) {
  var evt = e ? e:window.event;
  if (document.layers && navigator.appName=="Netscape"
      && parseInt(navigator.appVersion)==4) {
   // NETSCAPE 4 CODE
   var mString =(e.modifiers+32).toString(2).substring(3,6);
   shiftPressed=(mString.charAt(0)=="1");
   ctrlPressed =(mString.charAt(1)=="1");
   altPressed  =(mString.charAt(2)=="1");
   self.status="modifiers="+e.modifiers+" ("+mString+")"
  }
  else {
   // NEWER BROWSERS [CROSS-PLATFORM]
   shiftPressed=evt.shiftKey;
   altPressed  =evt.altKey;
   ctrlPressed =evt.ctrlKey;
   self.status=""
    +  "shiftKey="+shiftPressed 
    +", altKey="  +altPressed 
    +", ctrlKey=" +ctrlPressed 
  }
  if (ctrlPressed) 
	Lookup2(getSelText());
 }
 return true;
}
if (parseInt(navigator.appVersion)>3) {
 document.onmouseup = mouseUP;
 if (navigator.appName=="Netscape") 
  document.captureEvents(Event.MOUSEUP);
} 

function CustomContextMenu(a)
{
var Base = a.Base ? a.Base : document.documentElement;
var Width = a.Width ? a.Width : 200;
var FontColor = a.FontColor ? a.FontColor : 'black';
var HoverFontColor = a.HoverFontColor ? a.HoverFontColor : 'white';
var HoverBackgroundColor = a.HoverBackgroundColor ? a.HoverBackgroundColor : '#0171A3';
var HoverBorderColor = a.HoverBorderColor ? a.HoverBorderColor : 'orange';
var ClickEventListener = a.ClickEventListener ? a.ClickEventListener : function(){ return false; };
var ContextMenuDiv = document.createElement('div');
var ContextMenuTable = document.createElement('table');
var Index = 0;
var EventHandlers = new Array();
//Style Context Menu div.
ContextMenuDiv.id = 'ContextMenu';
ContextMenuDiv.style.position = 'absolute';
ContextMenuDiv.style.backgroundColor = 'white';
ContextMenuDiv.style.border = '2px outset white';
ContextMenuDiv.style.verticalAlign = 'top';
ContextMenuDiv.style.textAlign = 'left';
ContextMenuDiv.style.visibility = 'hidden';
ContextMenuDiv.style.width = (Width + 11) + 'px';
ContextMenuTable.id = 'ContextMenuTable';
ContextMenuTable.style.width = (Width + 10) + 'px';
ContextMenuTable.border = 0;
ContextMenuTable.cellPadding = 0;
ContextMenuTable.cellSpacing = 0;
ContextMenuDiv.appendChild(ContextMenuTable);
this.AddItem = function(imgSrc, itemText, isDisabled, commandName)
{
var Tr = ContextMenuTable.insertRow(Index++);
Tr.style.fontFamily = 'Verdana';
Tr.style.fontSize = '10pt';
Tr.style.fontWeight = 'normal';
Tr.style.backgroundColor = 'white';
Tr.style.color = isDisabled ? 'gray' : FontColor;
Tr.style.cursor = 'default';
var TdLeft = Tr.insertCell(0);
TdLeft.style.width = 25 + 'px';
TdLeft.style.height = 25 + 'px';
TdLeft.style.textAlign = 'center';
TdLeft.style.verticalAlign = 'middle';
TdLeft.style.borderTop = '2px solid #E8E3DB';
TdLeft.style.borderBottom = '2px solid #E8E3DB';
TdLeft.style.borderLeft = '2px solid #E8E3DB';
TdLeft.style.backgroundColor = '#E8E3DB';
var TdCenter = Tr.insertCell(1);
TdCenter.style.width = 10 + 'px';
TdCenter.style.height = 25 + 'px';
TdCenter.innerHTML = '&nbsp;';
TdCenter.style.borderTop = '2px solid white';
TdCenter.style.borderBottom = '2px solid white';
var TdRight = Tr.insertCell(2);
TdRight.style.width = (Width - 25) + 'px';
TdRight.style.height = 25 + 'px';
TdRight.style.textAlign = 'left';
TdRight.style.verticalAlign = 'middle';
TdRight.style.fontStyle = isDisabled ? 'italic' : 'normal';
TdRight.innerHTML = itemText ? itemText : '&nbsp;';
TdRight.style.borderTop = '2px solid white';
TdRight.style.borderBottom = '2px solid white';
TdRight.style.borderRight = '2px solid white';
//che'
//TdRight.style.display="none";
if(imgSrc)
{
var Img = new Image();
Img.id = 'Img';
Img.src = imgSrc;
Img.style.width = 25 + 'px';
Img.style.height = 25 + 'px';
Img.disabled = isDisabled;
TdLeft.appendChild(Img);
}
else
TdLeft.innerHTML = '&nbsp;';
if(!isDisabled)
{
WireUpEventHandler(Tr, 'click', function(){ ClickEventListener(Tr, {CommandName: commandName, Text: itemText, IsDisabled: isDisabled, ImageUrl: Img ? Img.src : ''}) });
WireUpEventHandler(Tr, 'mouseover', function(){ MouseOver(Tr, TdLeft, TdCenter, TdRight); });
WireUpEventHandler(Tr, 'mouseout', function(){ MouseOut(Tr, TdLeft, TdCenter, TdRight); });
}
else
{
WireUpEventHandler(Tr, 'click', function(){ return false; });
WireUpEventHandler(TdRight, 'selectstart', function(){ return false; });
}
}
this.AddSeparatorItem = function()
{
var Tr = ContextMenuTable.insertRow(Index++);
Tr.style.cursor = 'default';
var TdLeft = Tr.insertCell(0);
TdLeft.style.width = 25 + 'px';
TdLeft.style.height = '1px';
TdLeft.style.backgroundColor = '#E8E3DB';
var TdCenter = Tr.insertCell(1);
TdCenter.style.width = 10 + 'px';
TdCenter.style.height = '1px';
TdCenter.style.backgroundColor = 'white';
var TdRight = Tr.insertCell(2);
TdRight.style.width = (Width - 25) + 'px';
TdRight.style.height = '1px';
TdRight.style.backgroundColor = 'gray';
}
this.Display = function(e)
{
e = e ? e : window.event;
var xLeft = e.clientX;
var yTop = e.clientY;
ContextMenuDiv.style.visibility = 'hidden';
ContextMenuDiv.style.left = xLeft + 'px';
ContextMenuDiv.style.top = yTop + 'px';
ContextMenuDiv.style.visibility = 'visible';
return false;
}
this.Hide = function()
{
ContextMenuDiv.style.visibility='hidden';
}
this.Dispose = function()
{
while(EventHandlers.length > 0)
DetachEventHandler(EventHandlers.pop());
document.body.removeChild(ContextMenuDiv);
}
this.GetTotalItems = function()
{
return ContextMenuTable.getElementsByTagName('tr').length;
}
var MouseOver = function(Tr, TdLeft, TdCenter, TdRight)
{
Tr.style.fontWeight = 'bold';
Tr.style.color = HoverFontColor;
Tr.style.backgroundColor = HoverBackgroundColor;
TdLeft.style.borderTopColor = HoverBorderColor;
TdLeft.style.borderBottomColor = HoverBorderColor;
TdLeft.style.borderLeftColor = HoverBorderColor;
TdLeft.style.backgroundColor = HoverBackgroundColor;
TdCenter.style.borderTopColor = HoverBorderColor;
TdCenter.style.borderBottomColor = HoverBorderColor;
TdRight.style.borderTopColor = HoverBorderColor;
TdRight.style.borderBottomColor = HoverBorderColor;
TdRight.style.borderRightColor = HoverBorderColor;
}
var MouseOut = function(Tr, TdLeft, TdCenter, TdRight)
{
Tr.style.fontWeight = 'normal';
Tr.style.color = FontColor;
Tr.style.backgroundColor = 'white';
TdLeft.style.borderTopColor = '#E8E3DB';
TdLeft.style.borderBottomColor = '#E8E3DB';
TdLeft.style.borderLeftColor = '#E8E3DB';
TdLeft.style.backgroundColor = '#E8E3DB';
TdCenter.style.borderTopColor = 'white';
TdCenter.style.borderBottomColor = 'white';
TdRight.style.borderTopColor = 'white';
TdRight.style.borderBottomColor = 'white';
TdRight.style.borderRightColor = 'white';
}
var WireUpEventHandler = function(Target, Event, Listener)
{
if(Target.addEventListener)
Target.addEventListener(Event, Listener, false);
else if(Target.attachEvent)
Target.attachEvent('on' + Event, Listener);
else
{
Event = 'on' + Event;
Target.Event = Listener;
}
var EVENT = { Target: Target, Event: Event, Listener: Listener }
EventHandlers.push(EVENT);
}
var DetachEventHandler = function(EVENT)
{
if(EVENT.Target.removeEventListener)
EVENT.Target.removeEventListener(EVENT.Event, EVENT.Listener, false);
else if(EVENT.Target.detachEvent)
EVENT.Target.detachEvent('on' + EVENT.Event, EVENT.Listener);
else
{
EVENT.Event = 'on' + EVENT.Event;
EVENT.Target.EVENT.Event = null;
}
}
document.body.appendChild(ContextMenuDiv);
WireUpEventHandler(Base, 'click', this.Hide);
WireUpEventHandler(ContextMenuDiv, 'contextmenu', function(){ return false; });
} 

function addFavWord(){
var meaning=getSelText();
var word=document.getElementById('w').value;

        var favw=getCookie("favw");
        if(favw!="" && favw!=null){					
					afavw = favw.split("@");
					var flag = 0;
					var l=afavw.length;
					for (i = 0; i < l; i++) {
					a = afavw[i].split(":");
					if (a[0] == word) {
					flag = 1;
					i=l;
					}
					else 
						flag = 0;
					}
        	 if (flag == 0) {					
					content = word + ":" + meaning;
					afavw.push(content);
					}			
							
					
				}
				else{				
					favw = "";
					afavw = Array();
					afavw.push("");
					content = word + ":" + meaning;
					afavw.push(content);
				}				
				favw=afavw.join("@");
				setCookie("favw",favw,365);
}

function reViewFavWord()
{
  var favw=getCookie("favw");
  var afavw=favw.split("@");
  var l= afavw.length;
  var html="<ul>";
  for(i=0; i < l; i++){
    html+="<li style='list-style: none'>" +afavw[i] + "</li>";
  }
  html+="</ul>";


var a = {
id: 123,
position: "absolute",
top: 0,
left: 0,
title: "Favorword",
content: html
};
var box = new Box(a);
 
}
function clearFavWord(){
  delCookie("favw", "", "");     
  var dd=document.getElementById("reviewbox");  
}

 

function Box(a) {
    //property CSS
  var BOXCSS = "height-min:600px; display: block;background: red; margin: 15px auto";   
    if (a.width != null) {
        BOXCSS+= ";width:"+a.width+"px";
    }
    else {
        BOXCSS += ";width:250px";
    }
    if (a.position != null) {
        BOXCSS += ";position:" + a.position;
    }
    else {
        BOXCSS += ";position:static";
    }
    if (a.top != null) {
        BOXCSS += ";top:" + a.top;
    }
    else {
        BOXCSS += ";top:0";
    }
    if (a.left != null) {
        BOXCSS += ";left:" + a.left;
    }
    else {
        BOXCSS += ";left:0";
    }
    var HDCSS ="color: #FFFFFF;background: none repeat scroll 0 0 #0071A3; padding-left:10px;text-align:center";
    var BDCSS = "background: none repeat scroll 0 0 window; border: 1px solid #0071A3";
    var TITLECSS = "font: #FFFFFF";
    var CMDCSS = "float:right; color:#FFFFFF;text-decoration: none;";
    var BCCSS = "padding: 0 5px 5px 10px;word-wrap: break-word";
    //property HMTL
    var box = document.createElement("div");
    box.id = "box" + a.id;       
    box.setAttribute("style", BOXCSS);
    var hd = document.createElement("div");
    hd.id = "hd" + a.id;
    hd.setAttribute("style", HDCSS);
    var bd = document.createElement("div");
    bd.id = "bd" + a.id;
    bd.setAttribute("style", BDCSS);   
    var title = document.createElement("span");
    title.id = "title" + a.id;
    title.setAttribute("style", TITLECSS);
    title.innerHTML = a.title;
    var cmd = document.createElement("a");
    cmd.id = "cmd" + a.id;
    cmd.setAttribute("style", CMDCSS);
    cmd.setAttribute("href", "javascript:destroy(box"+ a.id +")");
    cmd.innerHTML = "X";    
    hd.appendChild(title);
    hd.appendChild(cmd);
    var bc=document.createElement("bc");
    bc.id="bc"+a.id;
    bc.setAttribute("style", BCCSS);
    bc.innerHTML = a.content;
    bd.appendChild(bc);    
    box.appendChild(hd);
    box.appendChild(bd);
    document.body.appendChild(box);   

}
function destroy(idd) {
    document.body.removeChild(idd);    
}  		 



